<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    //事件触发器
    class EventEmitter{
        //构造函数
        constructor(){
            //{'click':[fn1,fn2],'change':[fn]}
            this.subs = Object.create(null) //没有原型的对象
        }
        //注册事件
        $on(eventType,handler){
            //如果this.subs[eventType]有值，等于该值，如果没有值设为空数组
            this.subs[eventType]=this.subs[eventType] || []
            this.subs[eventType].push(handler)
        }
        //触发事件
        $emit(eventType){
            //如果有值
            if(this.subs[eventType]){
                this.subs[eventType].forEach(handler=>{
                    handler()
                })
            }
        }
    }

    //测试
    //事件中心
    let em = new EventEmitter()
    
    em.$on('click',()=>{
        console.log('click1')
    })
    em.$on('click',()=>{
        console.log('click2')
    })
    em.$emit('click')
</script>